/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.business;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.persistence.Parameter;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.modules.ivm.dao.business.ActivityProductDao;
import com.neusoft.lohas.modules.ivm.entity.business.ActivityProduct;
import com.neusoft.lohas.modules.ivm.entity.order.ShoppingCart;
import com.neusoft.lohas.modules.shop.dao.ShoppingCartDao;

/**
 * 商圈商店Service
 * @author Jackie.Liu
 * @version 2015-01-29
 */
@Component
@Transactional(readOnly = true)
public class ActivityProductService extends BaseService {

	@Autowired
	private ActivityProductDao activityProductDao;
	@Autowired
	private ShoppingCartDao shoppingCartDao;
	
	public ActivityProduct get(String id) {
		return activityProductDao.get(id);
	}
	
	public Page<ActivityProduct> find(Page<ActivityProduct> page, ActivityProduct activityProduct, String searchParam) {
		DetachedCriteria dc = activityProductDao.createDetachedCriteria();
		dc.add(Restrictions.eq(ActivityProduct.FIELD_DEL_FLAG, ActivityProduct.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", activityProduct.getMerchant().getId()));
		dc.add(Restrictions.eq("activity.id", activityProduct.getActivity().getId()));
		dc.add(Restrictions.eq("activityTime.id", activityProduct.getActivityTime().getId()));
		if (StringUtils.isNotEmpty(searchParam)){
			dc.createAlias("product", "product");
			if(isNumeric(searchParam)){
				dc.add(Restrictions.or(
					Restrictions.or(
						Restrictions.eq("product.code", Long.parseLong(searchParam))
					),
					Restrictions.or(
						Restrictions.like("product.name", "%"+searchParam+"%")
					)
				));
			}else{
				dc.add(Restrictions.or(
					Restrictions.or(
						Restrictions.like("product.name", "%"+searchParam+"%")
					)
				));
			}
		}
		dc.addOrder(Order.asc("sort"));
		return activityProductDao.find(page, dc);
	}
	
	public List<ActivityProduct> findList(ActivityProduct activityProduct, String searchParam) {
		DetachedCriteria dc = activityProductDao.createDetachedCriteria();
		dc.add(Restrictions.eq(ActivityProduct.FIELD_DEL_FLAG, ActivityProduct.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", activityProduct.getMerchant().getId()));
		dc.add(Restrictions.eq("activity.id", activityProduct.getActivity().getId()));
		dc.add(Restrictions.eq("activityTime.id", activityProduct.getActivityTime().getId()));
		if (StringUtils.isNotEmpty(searchParam)){
			dc.createAlias("product", "product");
			if(isNumeric(searchParam)){
				dc.add(Restrictions.or(
					Restrictions.or(
						Restrictions.eq("product.code", Long.parseLong(searchParam))
					),
					Restrictions.or(
						Restrictions.like("product.name", "%"+searchParam+"%")
					)
				));
			}else{
				dc.add(Restrictions.or(
					Restrictions.or(
						Restrictions.like("product.name", "%"+searchParam+"%")
					)
				));
			}
		}
		dc.addOrder(Order.asc("sort"));
		return activityProductDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ActivityProduct activityProduct) {
		activityProductDao.clear();
		activityProductDao.save(activityProduct);
	}
	
	@Transactional(readOnly = false)
	public void save(List<ActivityProduct> activityProducts) {
		activityProductDao.clear();
		activityProductDao.save(activityProducts);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		activityProductDao.deleteById(id);
	}
	
	public List<ShoppingCart> getCartList(String actProductId, String userId){
		String sql = "select * from ivm_shopping_cart a inner join ivm_business_panicbuy b on a.id=b.cart_id inner join ivm_business_activity_product c on b.activity_product_id=c.id where c.id=:p1 and a.user_id=:p2 and a.del_flag=:p3";
		return shoppingCartDao.findBySql(sql, new Parameter(actProductId, userId, ShoppingCart.DEL_FLAG_NORMAL), ShoppingCart.class);
	}
	
	/**
	 * 判断字符串是否为数字
	 */
	public boolean isNumeric(String str){ 
	   Pattern pattern = Pattern.compile("[0-9]*"); 
	   Matcher isNum = pattern.matcher(str);
	   if( !isNum.matches() ){
	       return false; 
	   } 
	   return true; 
	}

}
